package lgkj.meicqt20.ui;

import android.content.Intent;
import android.util.Log;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import lgkj.meicqt20.R;
import lgkj.meicqt20.adapter.VidoesNumAdapter;
import lgkj.meicqt20.bean.MessageEvent;
import lgkj.meicqt20.utils.AllKindsOfConstant;
import lgkj.meicqt20.utils.CommUtils;
import lgkj.meicqt20.utils.HttpUtils;
import lgkj.meicqt20.view.BannerViewpager;

/**
 * Created by Administrator on 2019/1/24 0024.
 */

public class VideosDetailsActivity extends BaseActivity {
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.video_little_banner)
    BannerViewpager videoLittleBanner;
    @BindView(R.id.iv_videos_img)
    ImageView ivVideosImg;
    @BindView(R.id.tv_vidoes_name)
    TextView tvVidoesName;
    @BindView(R.id.tv_vidoes_con)
    TextView tvVidoesCon;
    @BindView(R.id.gv_videos_num)
    GridView gvVideosNum;
    private VidoesNumAdapter adapter;
    private String video_id;
    private List<String> vodeosUrl = new ArrayList<>();
    private int video_time;

    @Override
    public int setContentView() {
        return R.layout.activity_videos_details;
    }

    @Override
    public void init() {
        activitiesList.add(this);
        displayDifferen();
        adapter = new VidoesNumAdapter(VideosDetailsActivity.this);
        gvVideosNum.setAdapter(adapter);
    }

    @Override
    public void initData() {
        video_id = getIntent().getStringExtra("video_id");
        if (video_id == null) {
            return;
        }
        Map<String, String> param = new HashMap();
        param.put("id", video_id);
        HttpUtils.getHttp(param, AllKindsOfConstant.getVideoNum, new HttpUtils.HttpCallBack() {
            @Override
            public void successHttp(JSONObject jsonObject, int id) {
                try {
                    if (jsonObject.getInt("status") == 1) {
                        vodeosUrl = new ArrayList<>();
                        JSONObject object = jsonObject.getJSONObject("data");
                        CommUtils.LoadImage(VideosDetailsActivity.this, ivVideosImg, object.optString("video_img"), R.drawable.common_icon_placeholder);
                        tvVidoesName.setText(object.getString("video_name"));
                        tvVidoesCon.setText(object.getString("video_text"));
                        video_time = object.optInt("video_time");
                        JSONArray jsonArray = object.getJSONArray("video_url");
                        if (jsonArray.length() > 0) {
                            for (int i = 0; i < jsonArray.length(); i++) {
                                String video_url = jsonArray.get(i).toString();
                                Log.d(TAG, jsonArray.get(0).toString());
                                vodeosUrl.add(video_url);
                            }
                            adapter.setData(vodeosUrl);
                            Log.d(TAG, "data.size:" + vodeosUrl.size() + ",adapter:" + adapter.getCount());
                            adapter.notifyDataSetChanged();
                            if (adapter.getCount() % Ynum == 0) {
                                countY = adapter.getCount() / Ynum;
                            } else {
                                countY = adapter.getCount() / Ynum + 1;
                            }

                        }
                    }
                } catch (Exception e) {
                    Log.d(TAG, "successHttp: " + e.getMessage());
                }
            }

            @Override
            public void errorHttp(String msg) {

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        EventBus.getDefault().register(this);
        HttpUtils.getQRCode(this, ivCode);
        Log.d(TAG, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(MessageEvent messageEvent) {
        Log.i(TAG, "socket: " + messageEvent.toString());
        if ("control".equals(messageEvent.getTagName())) {
            fragmentDealOrder(messageEvent.getMessage());
        }
    }

    private int position = 0;
    private int positionY = 0;
    private int Ynum = 20;
    private int countY = 0;

    private void fragmentDealOrder(String order) {
        switch (order) {
            case "up":
                if (positionY <= countY && positionY > 0) {
                    --positionY;
                    position = Ynum * positionY;
                    adapter.setmPositon(position);
                }
                break;
            case "down":
                if (positionY <= countY) {
                    ++positionY;
                    position = Ynum * positionY;
                    adapter.setmPositon(position);
                }
                if (position >= adapter.getCount()) {
                    position = adapter.getCount() - 1;
                    adapter.setmPositon(position);
                    positionY = countY;
                }
                break;
            case "sure":
                Intent intent = new Intent(VideosDetailsActivity.this, LoadUrlActivity.class);
                intent.putExtra("mMovieUrl", String.valueOf(adapter.getItem(position)));
                intent.putExtra("video_time", video_time);
                intent.putExtra("video_name",tvVidoesName.getText().toString());
                startActivity(intent);
                break;
            case "back":
                finish();
            case "left":
                if (position > 0) {
                    --position;
                    adapter.setmPositon(position);
                }
                break;
            case "right":
                if ((position + 1) < adapter.getCount()) {
                    ++position;
                    adapter.setmPositon(position);
                    if (((position / Ynum) - countY) == 0) {
                        positionY = countY;
                    }
                }
                break;
        }
        adapter.notifyDataSetChanged();
        Log.d(TAG, "收到指令:" + order + ",position:" + position + ",positionY:" + positionY + ",countY:" + countY + ",url:");

    }


}
